import { ElMessage } from 'element-plus'

export function sleep(millisecond = 500) {
  return new Promise((resolve) => {
    setTimeout(() => {
      return resolve(void 0);
    }, millisecond);
  });
}

export function $createCode(length = 6) {
  return Math.random().toString(36).replace(/\./g, "").slice(length * -1);
}
export function $createUid() {
  return 'UID-' + $createCode();
}

export function $objKey(obj, { keys, not, lib, from }, add) {
  const _data = {}
  if (not) {
    const notLib = new Set(not.split(','))
    Object.keys(obj).forEach(i => (!notLib.has(i) && (_data[i] = obj[i])))
  } else if (keys) {
    const keyLib = keys.split(',')
    const toLib = to ? to.split(',') : null
    const objkey = Object.keys(obj)
    keyLib.forEach((o, i) => {
      if (objkey.includes(o)) {
        if (toLib && toLib[i]) _data[toLib[i]] = obj[o]
        else _data[o] = obj[o]
      }
    })
  } else if (lib) {
    /**
     * @param {object} lib
     */
    Object.keys(lib).forEach(i => {
      _data[lib[i]] = obj[i] || null
    })
  } else if (from) {
    /**
     * @param {object} from
     */
    Object.keys(from).forEach(i => {
      _data[i] = obj[from[i]] || null
    })
  }
  if (add) Object.keys(add).forEach(i => (_data[i] = add[i]))
  return _data
}

export function $arr2obj(arr, { key, value, defaultValue = '' }) {
  const a = {}
  arr.forEach(i => (a[i[key]] = i[value] || defaultValue))
  return a
}

export function $obj2arr(obj, { key, value, valueType = 'string' }) {
  const a = Object.keys(obj).map(i => ({ [key]: i, [value]: valueType === 'number' ? Number(obj[i]) : obj[i] }))
  return a
}

export function clearArrKey(arr = [], key) {
  const _list = arr.map(i => ({
    ...i,
    [key]: clearArrKey(i[key], key)
  }))
  return arr.length === 0 ? null : _list
}

export async function clipWrite(text) {
  // console.log("copy", text);
  if (!text || typeof text !== 'string') return;
  if (navigator.clipboard) {
    await navigator.clipboard.writeText(text)
  } else {
    const clipInput = document.createElement("input");
    clipInput.value = text;
    document.body.appendChild(clipInput);
    clipInput.select();
    document.execCommand("copy");
    clipInput.remove();
  }
  ElMessage.success('已复制到剪切板')
}

const NumberLib = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
const CarryLib = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿'].reverse()
const DownLib = ['角', '分']
export function zh_number(num) {
  if (!Number(num)) return
  const arr = num.toString().split('.').map(i => i.split(''))
  const out = []
  const _Carry = CarryLib.slice(arr[0].length * -1)
  arr[0].forEach((i, idx) => out.push(`${NumberLib[Number(i)]}${Number(i) !== 0 ? _Carry[idx] : ''}`))
  if (out.last() === '零') out.pop()
  out.push('元')
  if (arr[1]) {
    const _down = arr[1].slice(0, 2)
    _down.forEach((i, idx) => out.push(`${NumberLib[Number(i)]}${DownLib[idx]}`))
    if (Number(_down.last()) === 0) out.pop()
  } else {
    out.push('整')
  }
  // console.log(out.join('').replace(/零+/g, '零'))
  return out.join('').replace(/零+/g, '零')
}

export function $stringHide(str, max = 10, type = -1, line = [3, 4]) {
  // console.log('$stringHide', str, max, type, line)
  if (str.length < max) return str
  const t = []
  switch (type) {
    case -1:
      t.push(str.slice(0, max))
      t.push('***')
      break;
    case 0:
      t.push(str.slice(max * -1))
      t.push('***')
      break;
    case 1:
      t.push(str.slice(0, line[0]))
      t.push('***')
      t.push(str.slice(line[0] * -1))
      break;
    default:
      t.push(str)
  }
  return t.join('')
}
